//****************************
//** CSpriteHigh
//**
//** High-Level sprite class
//**
//****************************

#pragma once

#include "stdafx.h"
#include "CSpriteLow.h"

#define NUM_FRAMES 256
#define MAX_SPRITES 1024

class CSpriteHigh: public CSpriteLow
{
private:
    int m_iCurFrame, m_iTotalFrames;

	BOOL m_bSaveBackground;

    HBITMAP m_hUnder;
    HDC m_hdc;

	// number of frames to wait for auto next frame,
	// and variable to track frame wait
	int m_iFrameRate;
	int m_iFrameCount; 
	BOOL m_bLoopFrames;

	int m_iFirstFrame;
	int m_iLastFrame;

	BOOL m_bIsACopy; // TRUE if this sprite was created by SpriteCopy'ing

	BOOL m_bIsStreched; // TRUE if this sprite is stretched
	int m_iSaveWidth, m_iSaveHeight;

	short m_iSpriteCollisionAccuracy;

	// Collision Sprite Array
	BOOL m_bUseCollisionArray;
	short m_iNumCollisionSprites;
	short m_iCollisionSprites[MAX_SPRITES];

public:
    CImageHigh *m_sprites[NUM_FRAMES];


    CSpriteHigh(HDC hdc);
    virtual ~CSpriteHigh();
    int LoadAnimSeq(int iStartX, int iStartY, int iNumFrames, LPTSTR lpFilename, int iWidth, int iHeight, int iFramesPerRow);

    virtual BOOL BitBlit();
    virtual BOOL StretchBlit(COLORREF clrTrans);
    virtual BOOL TransBlit(COLORREF clrTrans);
	virtual BOOL TransBlit(HDC hdc, int x, int y, COLORREF clrTrans);

    int ImageWidth() { return m_iWidth; };
    int ImageHeight() { return m_iHeight; };
    void SetImageSize(int iImgWidth, int iImgHeight);
    
    void SetFrame(int iFrame);
    int GetFrame() { return m_iCurFrame; };
    void NextFrame();
    void PrevFrame();

    void SetAutoFrameRate(int vNew) {m_iFrameRate = vNew;}
    int GetAutoFrameRate() { return m_iFrameRate; }
    void IncAutoFrameCount() {m_iFrameCount = (m_iFrameCount + 1) % m_iFrameRate;}
    int GetAutoFrameCount() { return m_iFrameCount; }
 
	void SetSaveBackGround(BOOL bSetSave);
	BOOL GetSaveBackGround();
 
	void SetFirstFrame(int iFirst) { m_iFirstFrame = iFirst; }
	int GetFirstFrame() { return m_iFirstFrame; }
	void SetLoopFrames(BOOL bVal) { m_bLoopFrames = bVal; }
	BOOL GetLoopFrames() { return m_bLoopFrames; }

	void SetLastFrame(int iLast) { m_iLastFrame = iLast; }
	int GetLastFrame() { return m_iLastFrame; }

    void SetTotalFrames(int iNew) { m_iTotalFrames = iNew; };
    int GetTotalFrames() { return m_iTotalFrames; };


	void SetIsACopy(BOOL bVal) { m_bIsACopy = bVal; }
	BOOL GetIsACopy() { return m_bIsACopy; }
	
	// Methods used for SpriteCopy'ing
	
	void SetSpriteCollisionAccuracy(short iVal) { m_iSpriteCollisionAccuracy = iVal; }
	short GetSpriteCollisionAccuracy() { return m_iSpriteCollisionAccuracy; }

	void SetFrameSource(int iFrameNum, CImageHigh* imgSrc) {
		// assign frame source (copy frames from other sprite)
		m_sprites[iFrameNum] = imgSrc;
		return;
	}
	
	CImageHigh * GetFrameSource(int iFrameNum) { 
		return m_sprites[iFrameNum]; 
	} 

	// stretching methods
	void SetStretch(BOOL bStretch, int iWidth, int iHeight) {
		m_bIsStreched = bStretch;
		
		// get rid of "saved under" image
		DeleteUnder();

		if (bStretch) {
			if (m_iSaveWidth==0 && m_iSaveHeight==0) {
				m_iSaveWidth = m_iWidth;
				m_iSaveHeight = m_iHeight;
			}
			m_iWidth = iWidth;
			m_iHeight = iHeight;
		} else {
			m_iWidth = m_iSaveWidth;
			m_iHeight = m_iSaveHeight;
		}
	}
	BOOL IsStretched() { return m_bIsStreched; }


	// Collision Sprite Array
	void SetCollisionArray(short *iCollisionSprites, short iNumCollisionSprites) {
		int i;
		for (i=0;i<iNumCollisionSprites;i++)
			m_iCollisionSprites[i] = iCollisionSprites[i];

		m_iNumCollisionSprites = iNumCollisionSprites;
		m_bUseCollisionArray = TRUE;
	}
	BOOL CheckCollisionBySprite() { return m_bUseCollisionArray; }
    BOOL ShouldFireCollision(short iSpriteNum) {
		int i;
		BOOL bFireCollision=FALSE;
		for (i=0; i<m_iNumCollisionSprites; i++) {
			if (m_iCollisionSprites[i] == iSpriteNum) {
				bFireCollision = TRUE;
				break;
			}
		}
		return bFireCollision;
	}
};


